package com.easy.sys.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.SysMenu;
import com.easy.common.service.IMenuService;
import com.easy.sys.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysMenuService implements IMenuService {
    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public SysMenu insert(SysMenu menu) {
        sysMenuMapper.insert(menu);
        SysMenu result=this.getByID(menu.getId());
        return result;
    }

    @Override
    public SysMenu update(SysMenu menu) {
        sysMenuMapper.updateById(menu);
        SysMenu result=this.getByID(menu.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int result=sysMenuMapper.deleteById(id);
        return result;
    }

    @Override
    public SysMenu getByID(Integer id) {
        SysMenu result=sysMenuMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<SysMenu> getListByPage(IPage page, SysMenu menu) {
        Wrapper wrapper = new QueryWrapper(menu);
        IPage<SysMenu> result=sysMenuMapper.selectPage(page, wrapper);
        return result;
    }

    @Override
    public List<SysMenu> getList(SysMenu menu) {
        Wrapper wrapper = new QueryWrapper(menu);
        List<SysMenu> result=sysMenuMapper.selectList(wrapper);
        return result;
    }

    @Override
    public int setVisible(SysMenu menu) {
        UpdateWrapper wrapper = new UpdateWrapper();
        if(menu.getVisible()==0){
            wrapper.set("visible",1);
        }else{
            wrapper.set("visible",0);
        }
        wrapper.eq("id",menu.getId());
        int result=sysMenuMapper.update(wrapper);
        return result;
    }
}
